<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use App\Service\PostService;
class PostController extends BaseAdminController{

    protected $postService;

    public function _init(){
        $this->postService = new PostService();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $params = $request->all();
        $data = $this->postService->getPostList($params);
        return view("admin.post.index", compact(['data']));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request) {
        if($request->isMethod("GET")){
            return view("admin.post.create");
        }else{
            $data = $request->except("_token");
            if(!empty($data['group'])){
                $data['group'] = explode("\r\n",$data['group']);
            }else{
                return $this->error(7000,"岗位名称不能为空");
            }
            $result = $this->postService->createPostData($data);
            if($result > 0){
                return $this->success($result,"添加成功");
            }else{
                return $this->error(7000,"添加失败");
            }
        }

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {

    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show() {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request) {
        if($request->isMethod("GET")){
            $id = $request->input('id');
            $data = $this->postService->findData($id);
            return view("admin.post.update",compact(['data']));
        }else{
            $data = $request->except("_token");
            if(empty($data['name'])){
                return $this->error(7000,"岗位名称不能为空");
            }
            $result = $this->postService->update($data['id'], $data);
            return $this->success($result, "编辑成功");
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $result = $this->postService->delete($id);
        return $this->success($result, "删除成功");
    }
    public function changeSort(Request $request) {
        $data = $request->except("_token");
        $result = $this->postService->changeSort($data);
        return $result;
    }
    public function groupIndex(Request $request){
        $params = $request->all();
        $data = $this->postService->getGroupList($params);
        return view("admin.post.group_index", compact(['data']));
    }
    public function groupCreate(Request $request){
        if($request->isMethod("GET")){
            return view("admin.post.group_create");
        }else{
            $data = $request->except("_token");
            if(empty($data['name'])){
                return $this->error(7000,"岗位分组名称不能为空");
            }
            $result = $this->postService->createGroup($data);
            return $this->success($result,"添加成功");
        }
    }
    public function groupUpdate(Request $request) {
        if($request->isMethod("GET")){
            $id = $request->input('id');
            $data = $this->postService->findGroupData($id);
            return view("admin.post.group_update",compact(['data']));
        }else{
            $data = $request->except("_token");
            $result = $this->postService->updateGroup($data['id'], $data);
            return $this->success($result, "编辑成功");
        }

    }
    public function changeGroupSort(Request $request) {
        $data = $request->except("_token");
        $result = $this->postService->changeGroupSort($data);
        return $result;
    }
    public function groupDestroy($id) {
        $list = $this->postService->selectData(["group_id"=>$id]);
        if(count($list) > 0){
            return $this->layuiDataError(7000,"请先删除岗位分组中的内容");
        }
        $result = $this->postService->groupDelete($id);
        return $this->success($result, "删除成功");
    }
}
